/*
 * @Author: Qimu
 * @Date: 2020-04-22 22:39:04
 * @LastEditors: Qimu
 * @LastEditTime: 2020-07-11 14:07:41
 * @Description: file content
 * @FilePath: \vue-admin-koa2\vue-admin\src\router\index.js
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
import utils from '@/plugins/utils'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', name: 'login', component: () => import('@/views/Login/Login.vue') },
  {
    path: '/index',
    name: 'home',
    component: () => import('@/views/Home/Index.vue'),
    redirect: '/index',
    children: [
      { path: '/index', component: () => import('@/views/Home/Home.vue') },
      { path: '/authority/menus', component: () => import('@/views/Author/Menus.vue') },
      { path: '/authority/roles', component: () => import('@/views/Author/Roles.vue') },
      { path: '/authority/admin', component: () => import('@/views/Author/Admins.vue') },
      { path: '/authority/button', component: () => import('@/views/Author/Button.vue') },
      { path: '/user/users', component: () => import('@/views/User/User.vue') },
      { path: '/article/articleList', component: () => import('@/views/Article/Article.vue') },
      { path: '/article/addArticle', component: () => import('@/views/Article/AddArticle.vue') },
      { path: '/article/categories', component: () => import('@/views/Article/ArticleCategory.vue') },
      { path: '/article/articleDetail/:articleId', component: () => import('@/views/Article/ArticleDetail.vue') },
      { path: '/orders', component: () => import('@/views/Order/Order.vue') },
      { path: '/401', component: () => import('@/views/Error/401.vue') }
    ]
  },
  { path: '*', component: () => import('@/views/Error/404.vue') }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  scrollBehavior: () => ({ y: 0 }),
  routes
})

//路由导航守卫控制登录权限
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = utils.local.get('userToken')
  return !tokenStr ? next('/login') : next();
})

export default router